package com.syyo.gulimall.product.web;

import com.syyo.gulimall.product.domain.entity.CategoryEntity;
import com.syyo.gulimall.product.domain.vo.Catelog2Vo;
import com.syyo.gulimall.product.service.CategoryService;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @ author:  wang
 * @ date: 2021/3/25 10:01
 * @ Description: 首页
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        // 查出所有的1级分类
        List<CategoryEntity> list = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", list);
        return "index";
    }

    /**
     *
     */
    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatelogJson() {
        // 查出所有的1级分类
        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }

    /**
     * 测试性能用的
     * redis锁
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        // 获取锁
        RLock lock = redissonClient.getLock("my-lock");
        /**
         *    lock.lock();方法会有自动续期的功能
         *         加锁,默认30秒，锁自动续期，自动解锁
         *   lock.lock(30L, TimeUnit.SECONDS);
         *   没有自动续期功能
         */
        lock.lock();
        try {
            System.out.println("加锁成功，执行业务: " + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            System.out.println("解锁成功: " + Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }


    /**
     * redis信号量，等其他锁都释放了才执行
     */
    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {

        RSemaphore park = redissonClient.getSemaphore("park");
        park.acquire();// 获取一个信号，阻塞方法
//        park.tryAcquire();// 获取一个信号，不阻塞
        return "ok";
    }

    /**
     *
     */
    @ResponseBody
    @GetMapping("/go")
    public String go() {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();// 释放一个信号
        return "ok";
    }

    /**
     * redis闭锁，等其他锁都释放了才执行
     */
    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        // 获取锁
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放学关门了";
    }

    /**
     *
     */
    @ResponseBody
    @GetMapping("/go/go/{id}")
    public String gogo(@PathVariable("id") Integer id) {
        // 获取锁
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id + "班的人走了";
    }
}
